package com.zretc.device.ui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import com.zretc.dbtools.RandomIdTools;
import com.zretc.device.dao.Device_NeedDao;
import com.zretc.device.entity.Device_Need;
import com.zretc.device.entity.User;

import javax.swing.JLabel;
import javax.swing.JOptionPane;

import java.awt.Font;
import javax.swing.JTextField;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Device_needAddDialog extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private JTextField txtdepartment;
	private JTextField txtdevice_code;
	private JTextField txtneed_num;
	private JTextField txtbegin_date;
	private JTextField txtneed_person;
	private JTextField txtend_date;
	private JTextField txtneed_tele;
	private JTextField txtneed_id;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			Device_needAddDialog dialog = new Device_needAddDialog();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	public Device_needAddDialog() {
		setBounds(100, 100, 781, 599);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);

		JLabel label = new JLabel("申请设备");
		label.setFont(new Font("宋体", Font.PLAIN, 40));
		label.setBounds(290, 13, 182, 47);
		contentPanel.add(label);

		JLabel label_8 = new JLabel("需求单号");
		label_8.setFont(new Font("宋体", Font.PLAIN, 25));
		label_8.setBounds(182, 79, 120, 30);
		contentPanel.add(label_8);

		txtneed_id = new JTextField();
		txtneed_id.setText(RandomIdTools.getRan());
		txtneed_id.setEditable(false);
		txtneed_id.setFont(new Font("宋体", Font.PLAIN, 25));
		txtneed_id.setColumns(10);
		txtneed_id.setBounds(316, 79, 250, 30);
		contentPanel.add(txtneed_id);

		txtdepartment = new JTextField();
		txtdepartment.setFont(new Font("宋体", Font.PLAIN, 25));
		txtdepartment.setColumns(10);
		txtdepartment.setBounds(316, 122, 250, 30);
		contentPanel.add(txtdepartment);

		JLabel label_1 = new JLabel("使用部门");
		label_1.setFont(new Font("宋体", Font.PLAIN, 25));
		label_1.setBounds(182, 122, 120, 30);
		contentPanel.add(label_1);

		txtdevice_code = new JTextField();
		txtdevice_code.setFont(new Font("宋体", Font.PLAIN, 25));
		txtdevice_code.setColumns(10);
		txtdevice_code.setBounds(316, 168, 250, 30);
		contentPanel.add(txtdevice_code);

		JLabel label_2 = new JLabel("设备编号");
		label_2.setFont(new Font("宋体", Font.PLAIN, 25));
		label_2.setBounds(182, 168, 120, 30);
		contentPanel.add(label_2);

		txtneed_num = new JTextField();
		txtneed_num.setFont(new Font("宋体", Font.PLAIN, 25));
		txtneed_num.setColumns(10);
		txtneed_num.setBounds(316, 211, 250, 30);
		contentPanel.add(txtneed_num);

		JLabel label_3 = new JLabel("需求数量");
		label_3.setFont(new Font("宋体", Font.PLAIN, 25));
		label_3.setBounds(182, 211, 120, 30);
		contentPanel.add(label_3);

		txtbegin_date = new JTextField();
		txtbegin_date.setFont(new Font("宋体", Font.PLAIN, 25));
		txtbegin_date.setColumns(10);
		txtbegin_date.setBounds(316, 254, 250, 30);
		contentPanel.add(txtbegin_date);

		JLabel label_4 = new JLabel("开始日期");
		label_4.setFont(new Font("宋体", Font.PLAIN, 25));
		label_4.setBounds(182, 254, 133, 30);
		contentPanel.add(label_4);

		txtneed_person = new JTextField();
		//设置为不可编辑
		txtneed_person.setEditable(false);
		//给文本框赋值
		User user =User.getInfo();//获取输入的值
		txtneed_person.setText(user.getUsername());
		txtneed_person.setFont(new Font("宋体", Font.PLAIN, 25));
		txtneed_person.setColumns(10);
		txtneed_person.setBounds(316, 340, 250, 30);
		contentPanel.add(txtneed_person);

		JLabel label_5 = new JLabel("结束日期");
		label_5.setFont(new Font("宋体", Font.PLAIN, 25));
		label_5.setBounds(182, 297, 106, 30);
		contentPanel.add(label_5);

		txtend_date = new JTextField();
		txtend_date.setFont(new Font("宋体", Font.PLAIN, 25));
		txtend_date.setColumns(10);
		txtend_date.setBounds(316, 297, 250, 30);
		contentPanel.add(txtend_date);

		JLabel label_6 = new JLabel("用 户 名");
		label_6.setFont(new Font("宋体", Font.PLAIN, 25));
		label_6.setBounds(182, 340, 106, 30);
		contentPanel.add(label_6);

		JLabel label_7 = new JLabel("用户电话");
		label_7.setFont(new Font("宋体", Font.PLAIN, 25));
		label_7.setBounds(182, 383, 133, 30);
		contentPanel.add(label_7);

		txtneed_tele = new JTextField();
		txtneed_tele.setFont(new Font("宋体", Font.PLAIN, 25));
		txtneed_tele.setColumns(10);
		txtneed_tele.setBounds(316, 383, 250, 30);
		contentPanel.add(txtneed_tele);

		JButton button = new JButton("取消");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				txtneed_id.setText(RandomIdTools.getRan());
				txtdepartment.setText("");
				txtdevice_code.setText("");
				txtneed_num.setText("");
				txtbegin_date.setText("");
				txtend_date.setText("");
				
				txtneed_tele.setText("");
			}
		});
		button.setFont(new Font("宋体", Font.PLAIN, 25));
		button.setBounds(208, 445, 150, 38);
		contentPanel.add(button);

		JButton button_1 = new JButton("添加");
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// 需求id
				String tneed_id = txtneed_id.getText();
				// 需求部门
				String department = txtdepartment.getText();
				// 设备编号
				String device_code = txtdevice_code.getText();
				// 需求数量
				String tneed_num = txtneed_num.getText();
				// 开始时间
				String begin_date = txtbegin_date.getText();
				// 结束时间
				String end_date = txtend_date.getText();
				// 申请人
				String need_person = txtneed_person.getText();
				// 申请人电话
				String tneed_tele = txtneed_tele.getText();

				// 判断输入的值
				if (tneed_id.equals("")) {
					// 弹窗提示
					JOptionPane.showMessageDialog(null, "编号不能为空");
				} else if (department.equals("")) {
					// 弹窗提示
					JOptionPane.showMessageDialog(null, "使用部门不能为空");
				} else if (device_code.equals("")) {
					// 弹窗提示
					JOptionPane.showMessageDialog(null, "设备编号不能为空");
				} else if (tneed_num.equals("")) {
					// 弹窗提示
					JOptionPane.showMessageDialog(null, "需求数量不能为空");
				} else if (begin_date.equals("")) {
					// 弹窗提示
					JOptionPane.showMessageDialog(null, "起始日期不能为空");
				} else if (end_date.equals("")) {
					// 弹窗提示
					JOptionPane.showMessageDialog(null, "结束日期不能为空");
				} else if (need_person.equals("")) {
					// 弹窗提示
					JOptionPane.showMessageDialog(null, "需求人不能为空");
				} else if (tneed_tele.equals("")) {
					// 弹窗提示
					JOptionPane.showMessageDialog(null, "需求人电话不能为空");
				} else if (tneed_tele.length() != 11) {
					// 弹窗提示
					JOptionPane.showMessageDialog(null, "需求人电话长度错误");
				} else {
					//使用正则
					if (tneed_tele.matches("^((13[0-9])|(15[^4])|(17[^9])|(18[^14]))\\d{8}$")) {
						// 需求id
						int need_id = Integer.parseInt(tneed_id);
						// 需求数量
						int need_num = Integer.parseInt(tneed_num);
						// 创建对象
						Device_Need dn = new Device_Need(need_id, department, device_code, need_num, begin_date, end_date,
								need_person, tneed_tele);
						Device_NeedDao dnd = new Device_NeedDao();
						boolean flag = dnd.addDevNeed(dn);
						if (flag) {
							// 弹窗提示
							JOptionPane.showMessageDialog(null, "添加成功");
						} else {
							// 弹窗提示
							JOptionPane.showMessageDialog(null, "添加失败,该设备已进行申请，请勿重复添加");
						}

					}else {
						// 弹窗提示
						JOptionPane.showMessageDialog(null, "需求人电话不是一个正确的号码");
					}
					
				}
			}
		});
		button_1.setFont(new Font("宋体", Font.PLAIN, 25));
		button_1.setBounds(403, 445, 150, 38);
		contentPanel.add(button_1);

		// 居中
		setLocationRelativeTo(null);
	}
}
